import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


/**
 * This class creates  an interface for an agency or an Employer
 * Final Project for TCSS 445.
 * 
 * @author Sanjeev Kamboj
 */
@SuppressWarnings("serial")
public class JobPostingPanel extends JPanel {
	private static final int NUM_COLUMNS = 20;
	
	private User my_user;
	
	private SqlDao my_dao;
	private JLabel position;
	private JLabel industry;
	private JLabel open_date;
	private JLabel close_date;
	private JLabel min_qual;
	private JLabel job_location;
	private JLabel min_salary;
	private JLabel max_salary;
	private JLabel how_apply;
	
	private JTextField pos_field;
	private JTextField industry_field;
	private JTextField open_date_field;
	private JTextField close_date_field;
	private JTextArea min_qual_field;
	private JTextArea job_location_field;
	private JTextField min_salary_field;
	private JTextField max_salary_field;
	private JTextArea how_apply_field;
	private JFrame my_frame;
	
	private JButton submit_button;
	private JButton cancel_button;
	
	
	public JobPostingPanel(final JFrame the_frame, final SqlDao the_dao, final User user) {
		super(new BorderLayout());
		
		my_frame = the_frame;
		my_dao = the_dao;
		my_user = user;
		
		//create labels
		position = new JLabel("Postion Title: ");
		industry = new JLabel("Industry: ");
		open_date = new JLabel("Open Date: ");
		close_date = new JLabel("Close Date: ");
		min_qual = new JLabel("Minimum Qualifications: ");
		job_location = new JLabel("Job Location: ");
		min_salary = new JLabel("Minimum Salary: ");
		max_salary = new JLabel("Maximum Salary: ");
		how_apply = new JLabel("How to Apply: ");

 
		pos_field = new JTextField(NUM_COLUMNS);
	    industry_field = new JTextField(NUM_COLUMNS);
		open_date_field = new JTextField(NUM_COLUMNS);
		close_date_field = new JTextField(NUM_COLUMNS);
		min_qual_field = new JTextArea();
		min_qual_field.setBorder(BorderFactory.createLineBorder(Color.black));
		
		job_location_field = new JTextArea();
		job_location_field.setBorder(BorderFactory.createLineBorder(Color.black));
		
		min_salary_field = new JTextField(NUM_COLUMNS);
		max_salary_field = new JTextField(NUM_COLUMNS);
		
		how_apply_field = new JTextArea();
		how_apply_field.setBorder(BorderFactory.createLineBorder(Color.black));
		
 
        //Tell accessibility tools about label/textfield pairs.
        position.setLabelFor(pos_field);
        industry.setLabelFor(industry_field);
        open_date.setLabelFor(open_date_field);
        close_date.setLabelFor(close_date_field);
        min_qual.setLabelFor(min_qual_field);
        job_location.setLabelFor(job_location_field);
        min_salary.setLabelFor(min_salary_field);
        max_salary.setLabelFor(max_salary_field);
        how_apply.setLabelFor(how_apply_field);
        

        //Lay out the labels in a panel.
        JPanel labelPane = new JPanel(new GridLayout(0,1));
        
        labelPane.add(position);
        labelPane.add(industry);
        labelPane.add(open_date);
        labelPane.add(close_date);
        labelPane.add(min_qual);
        labelPane.add(job_location);
        labelPane.add(min_salary);
        labelPane.add(max_salary);
        labelPane.add(how_apply);
        
 
        //Layout the text fields in a panel.
        JPanel fieldPane = new JPanel(new GridLayout(0,1));
        fieldPane.add(pos_field);
        fieldPane.add(industry_field);
        fieldPane.add(open_date_field);
        fieldPane.add(close_date_field);
        fieldPane.add(min_qual_field);
        fieldPane.add(job_location_field);
        fieldPane.add(min_salary_field);
        fieldPane.add(max_salary_field);
        fieldPane.add(how_apply_field);
 
        //Put the panels in this panel, labels on left,
        //text fields on right.
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        add(labelPane, BorderLayout.WEST);
        add(fieldPane, BorderLayout.CENTER);
	

        //Layout the text fields in a panel.
        JPanel buttonPane = new JPanel(new FlowLayout());
        submit_button = new JButton("Submit");
        cancel_button = new JButton("Cancel");
        
        submit_button.addActionListener(new SubmitJobListener());
        cancel_button.addActionListener(new ListenCancel());
        
        buttonPane.add(submit_button);
        buttonPane.add(cancel_button);
        
        add(buttonPane, BorderLayout.SOUTH);
	}
	
	   private class SubmitJobListener implements ActionListener{
	        public void actionPerformed(ActionEvent e){
	        	
	        	String min_sal = min_salary_field.getText();
	        	min_sal = min_sal.replace(",", "");
	        	min_sal = min_sal.replace("$", "");
	        	
	        	String max_sal = max_salary_field.getText();
	        	max_sal = max_sal.replace(",", "");
	        	max_sal = max_sal.replace("$", "");
	        	
	        	my_dao.insertJobPosting(pos_field.getText(), 
	        							industry_field.getText(), 
	        							open_date_field.getText(), 
	        							close_date_field.getText(), 
	        							min_qual_field.getText(), 
	        							job_location_field.getText(), 
	        							Double.parseDouble(min_sal), 
	        							Double.parseDouble(max_sal), 
	        							how_apply_field.getText(), 
	        						    my_user.getID());
	        	
	        	my_frame.remove(JobPostingPanel.this);
	        	my_frame.repaint();
	        	 my_frame.setVisible(true);
	        	
	        }
	    }
	    private class ListenCancel implements ActionListener{
	        public void actionPerformed(ActionEvent e){
	                my_frame.remove(JobPostingPanel.this);
	                
	                my_frame.repaint();
	                my_frame.setVisible(true);
	        }
	    }

}
